package msu.ml.data;

import msu.ml.util.*;
import msu.ml.data.level2.*;
import msu.ml.core.*;
import weka.core.*;
import java.io.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

/**
 * The Level2DataLoader is a DataLoader capable of loading
 * compressed Nexrad Level II data files.
 *
 * @author Reginald M Mead
 * @version 1.0
 */
public class Level2DataLoader implements IDataLoader 
{
	/** 
	 * Creates a new Level2DataLoader. By default the loader has
	 * a depth of 1000m and uses an ExtendedPulseVolumeFactory with
	 * window size of 3 and depth of 1000m.
	 */
	public Level2DataLoader()
	{

	}

	/**
	 * Returns true if the file is a compress Nexrad
	 * Level II file
	 *
	 * @param target file to load
	 * @return true if file can be loaded
	 */
	public boolean isSupportedData(String target)
	{
		return target.endsWith(".Z") || target.endsWith(".gz");
	}

	/**
	 * Load the specified Nexrad Level II File
	 *
	 * @param target file to load
	 * @return loaded file
	 */
	public NxInstances load(String target)
	{
      VolumeReader reader = new VolumeReader(new BlockReader(target));
      Volume v = reader.readVolume();
      reader.close();

      PreProcessor pp = new PreProcessor();
      pp.setMinRange(20000);

      Scan rscan = pp.process(v.getSweep(0).getScan(0));
      Scan dscan = pp.process(v.getSweep(0).getScan(1));

      ScanDataConverter converter = new ScanDataConverter();
      converter.setConvolutionWidth(25);
      Instances inst = converter.convertToInstances(target, rscan, dscan,
         true, true, true);

      return new NxInstances(inst, target);
	}
}
